package com.meihua.shellapp.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.meihua.shellapp.R;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.BaseBean;
import com.meihua.shellapp.utils.FastClickUtils;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;

import butterknife.Bind;

/**
 * 找回密码
 */

public class SetNewPwdActivity extends BaseActivity {

    @Bind(R.id.iv_top_left)
    ImageView iv_top_left;//返回
    @Bind(R.id.tv_title)
    TextView tv_title;//标题

    @Bind(R.id.et_newpwd)
    EditText et_newpwd;//新密码
    @Bind(R.id.et_newpwd_again)
    EditText et_newpwd_again;//确认新密码
    @Bind(R.id.bt_finish)
    Button bt_finish;//完成

    private String phone;

    @Override
    public int getLayoutId() {
        return R.layout.activity_set_new_pwd;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        phone = getIntent().getStringExtra("phone");
        tv_title.setVisibility(View.VISIBLE);
        iv_top_left.setVisibility(View.VISIBLE);
        iv_top_left.setOnClickListener(this);
        tv_title.setText("新密码");
        bt_finish.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_finish://完成
                if(FastClickUtils.isFastClick()) {
                    if (et_newpwd.getText().toString().trim().length() < 6) {
                        ToastUtils.getInstance().showToast("密码长度最少6位");
                        return;
                    }
                    if (!et_newpwd.getText().toString().equals(et_newpwd_again.getText().toString())) {
                        ToastUtils.getInstance().showToast("两次密码输入不一致，请重新输入！");
                        return;
                    }
                    saveData(et_newpwd.getText().toString().trim());
                }
                break;
        }
    }

    /**
     * 提交
     *
     * @param et_newpwd
     */
    private void saveData(String et_newpwd) {
        OkGo.<String>post(URL.getUrl().UPDATEPASSWORD)
                .params("phone", phone)
                .params("password", et_newpwd)
                .params("type", 2)
                .execute(new MyStringCallBack<BaseBean>(bt_finish) {
                    @Override
                    public void onSuccessData(BaseBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getCode() == 1) {
                            finish();
                        }
                        ToastUtils.getInstance().showToast(bean.getMsg());
                    }
                });
    }
}
